package com.moecollege.umiao.frame;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;

import com.moecollege.moecollegelib.adapter.FramePagerAdapter;
import com.moecollege.moecollegelib.base.BaseFragment;
import com.moecollege.umiao.R;
import com.moecollege.umiao.controller.MainFrameCotroller;

import java.util.List;


/**
 * 主框架页面
 * Created by natsuki on 15/6/4.
 */
public class MainFrameActivity extends UmiaoActivity {
    private Toolbar mToolBar;
    private AppBarLayout barLayout;
    private TabLayout tabLayout;
    private ViewPager tabViewPager;
    private List<String> mTitles;
    private List<BaseFragment> mPagers;
    private RelativeLayout headLayout;
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_frame);
        mToolBar = (Toolbar) findViewById(R.id.toolBar);
        barLayout = (AppBarLayout) findViewById(R.id.barlayout);
        headLayout = (RelativeLayout) findViewById(R.id.head_layout);
        navigationView = (NavigationView) findViewById(R.id.navigation);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
        setSupportActionBar(mToolBar);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabViewPager = (ViewPager) findViewById(R.id.view_pager);
        mTitles = MainFrameCotroller.getTabTilte();
        mPagers = MainFrameCotroller.getPagers();
        int size = mPagers.size();
        for (int i = 0; i < size; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(mTitles.get(i)));
        }
        FragmentPagerAdapter pagerAdapter = getPagerApdater();
        tabViewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(tabViewPager);
        tabLayout.setTabsFromPagerAdapter(pagerAdapter);

        mToolBar.setLogo(R.drawable.ic_user_head);
        mToolBar.setNavigationIcon(R.drawable.ic_drawer);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }


    private FragmentPagerAdapter getPagerApdater() {
        return new FramePagerAdapter(getSupportFragmentManager(), mTitles, mPagers);
    }


}
